package sim.ring.gui.dialogs;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;

import sim.ring.gui.RingingSimApp;
import sim.ring.gui.databinding.ConfigData;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.BeanProperties;

public class ConfigAppDialog extends TitleAreaDialog {
	private DataBindingContext m_bindingContext;

	RingingSimApp r;
	private static Logger log = Logger.getLogger("sim.ring.gui.dialogs");
	private Text text;
	
	ConfigData cfg;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public ConfigAppDialog(RingingSimApp parentApp) {
		super(parentApp.getShell());
		r = parentApp;
		cfg = new ConfigData();
		cfg.setGuiLanguage(r.getConfigData("GUILanguage"));
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayout(new FormLayout());
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		CTabFolder tabFolder = new CTabFolder(container, SWT.BORDER);
		FormData fd_tabFolder = new FormData();
		fd_tabFolder.bottom = new FormAttachment(0, 153);
		fd_tabFolder.right = new FormAttachment(0, 444);
		fd_tabFolder.top = new FormAttachment(0);
		fd_tabFolder.left = new FormAttachment(0);
		tabFolder.setLayoutData(fd_tabFolder);
		tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
		
		CTabItem tbtmGeneral = new CTabItem(tabFolder, SWT.NONE);
		tbtmGeneral.setText(r.getLocalisedData("configDialogGeneralTab", "General"));
		
		Composite composite = new Composite(tabFolder, SWT.NONE);
		tbtmGeneral.setControl(composite);
		composite.setLayout(new GridLayout(2, false));
		
		Label lblLanguage = new Label(composite, SWT.NONE);
		lblLanguage.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblLanguage.setText(r.getLocalisedData("configDialogLanguageSetting","Language"));
		
		text = new Text(composite, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		return area;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button button = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				log.debug("OK button pressed");
				r.updateConfigData("GUILanguage", cfg.getGuiLanguage(), true);
			}
		});
		
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
		m_bindingContext = initDataBindings();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
	
	@Override
	public void create() {
		super.create();
		log.debug("Config dialog creation started");
		setTitle(r.getLocalisedData("configDialogTitle", "Configure application"));
		setMessage(r.getLocalisedData("configDialogInitMessage", "Change the settings of the application ..."));
		//cfg.setGuiLanguage(r.getConfigData("GUILanguage"));
	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue observeTextTextObserveWidget = WidgetProperties.text(SWT.Modify).observe(text);
		IObservableValue guiLanguageCfgObserveValue = BeanProperties.value(ConfigData.class,"guiLanguage").observe(cfg);
		bindingContext.bindValue(observeTextTextObserveWidget, guiLanguageCfgObserveValue, null, null);
		//
		return bindingContext;
	}
}
